<template>
  <div class="app-container">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <!--<el-input v-model="searchParamValue.SEARCH_LIKE_value" placeholder="请输入关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilterValue" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilterValue">搜索</el-button>-->
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreateValue">新增</el-button>
    </div>

    <!-- 表格-内容区域 -->
    <el-table v-loading="listLoadingValue" :data="tableListValue" border fit highlight-current-row style="width: 100%;height: 250px">
      <el-table-column
        v-for="column in columnValues"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align?column.align:'center'"
        :min-width="column.minWidth"
        :sortable="column.sortable"
        :fixed="column.fixed"
        :type="column.type"
        :formatter="column.formatter">
        <template slot-scope="scope">
          <img v-if="column.imgFlag==true" :src="CMBASE.baseFileContext + scope.row[column.prop]" width="60" height="60" class="img">
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1" :inactive-value="0" disabled />
          <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>
      <!-- 操作-->
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdateValue(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDeleteValue(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格-页脚区域 -->
    <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize" @pagination="getList" />

    <!--新增编辑key对应value界面-->
    <el-dialog :title="operationValue?'新增':'编辑'" :visible.sync="dialogFormVisibleValue" :modal-append-to-body="false" :modal="false" :close-on-click-modal="false" width="45%" top="15vh" >
      <div>
        <el-form ref="dataValueForm" :model="dataValueForm" :rules="dataValueFormRules" label-width="140px" style="width: 80%">
          <el-form-item label="属性值" prop="value">
            <el-input v-model="dataValueForm.value" auto-complete="off" />
          </el-form-item>
          <el-form-item label="排序号码" prop="orderNo">
            <el-input v-model="dataValueForm.orderNo" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataValueForm.remark" auto-complete="off" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialogValue()">取消</el-button>
        <el-button :loading="editLoadingValue" size="mini" type="primary" @click.native="saveFormDataValue()">提交
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination/index' // Secondary package based on el-pagination
import { getMSpecValueList, saveMSpecValueInfo, deleteMSpecValueInfo } from '@/api/modules/goodsInfo'

export default {
  name: 'ProductBrand',
  components: { Pagination },
  props: {
    rowGroupKeyId: {
      type: [String],
      default: ''
    }
  },
  directives: { waves },
  filters: { },
  data() {
    return {
      tableKey: 0,
      tableList: [], // 表格数据源
      tableTotal: 0, // 表格总条数数据
      treeTableDataList: [],
      specGroupList: [], // 规格组数组
      pageRequest: { pageNum: 1, pageSize: 10 },
      searchParam: { SEARCH_LIKE_spec_name: '' },
      dialogFormVisible: false, // 默认不显示弹出框
      operation: true, // true:新增, false:编辑
      editLoading: false, // 保存表单时loading框
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      // 商品spu 属性key对应value
      valueListName: '属性值列表',
      getValueListVisible: false, // 属性值列表页 隐藏
      operationValue: true, // true:新增, false:编辑
      searchParamValue: { SEARCH_LIKE_speckey_id: '', SEARCH_LIKE_value: '' }, // 属性key对应value 搜索条件
      dialogFormVisibleValue: false, // 默认不显示弹出框
      listLoadingValue: true,
      tableListValue: [], // 表格数据源
      columnValues: [
        { prop: 'value', label: '属性值名称', sortable: false },
        { prop: 'orderNo', label: '排序', sortable: false },
        // { prop: 'remark', label: '备注', sortable: false },
        { prop: 'createTime', label: '创建时间', sortable: false }
      ],
      // 新增编辑 Value界面数据
      dataValueForm: {},

      // Value表单检验规则
      dataValueFormRules: {
        value: [{ required: true, message: '请输入属性值名称', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }],
        orderNo: [{ required: true, message: '请输入排序号码', trigger: 'blur' }]
      },
      editLoadingValue: false, // 保存表单时loading框
      searchParamSpecGrp: { SEARCH_LIKE_classify_id: '' } // 根据分类id获取规格组 搜索条件
    }
  },
  created() {
    this.getList(this.rowGroupKeyId)
  },
  methods: {
    // 获取表格数据
    getList(id) {
      this.listLoadingValue = true
      // 【API请求】----获取用户列表数据
      this.searchParamValue.SEARCH_LIKE_speckey_id = id
      this.pageRequest.searchParam = this.searchParamValue
      getMSpecValueList(this.pageRequest).then(response => {
        this.tableListValue = response.result.content
        this.tableTotal = response.result.total
        this.listLoadingValue = false
      }).catch((res) => {
        this.$message({ message: '用户列表获取失败', type: 'error' })
      })
    },
    // 属性值表格数据筛选
    handleFilterValue() {
      var id = this.searchParamValue.SEARCH_LIKE_speckey_id
      var value = this.searchParamValue.SEARCH_LIKE_value
      this.getList(id, value)
    },

    // 【属性值列表 按钮点击】--- 弹出新增表单
    handleCreateValue() {
      this.operationValue = true
      this.initDataValueForm()
      this.dataValueForm.speckeyId = this.searchParamValue.SEARCH_LIKE_speckey_id
      this.dialogFormVisibleValue = true
      this.checkedIds = []
      this.$nextTick(() => {
        this.$refs['dataValueForm'].clearValidate()
        this.editLoadingValue = false
      })
    },
    // 【属性值列表 按钮点击】--- 弹出编辑表单
    handleUpdateValue(row) {
      this.operationValue = false
      this.dataValueForm = Object.assign({}, row)
      // 显示编辑菜单
      this.dialogFormVisibleValue = true
      this.$nextTick(() => {
        this.$refs['dataValueForm'].clearValidate()
        this.editLoadingValue = false
      })
    },
    // 属性值 保存表单数据
    saveFormDataValue() {
      this.$refs['dataValueForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.editLoadingValue = true
        // 【API请求】----保存数据
        saveMSpecValueInfo(this.dataValueForm).then(() => {
          this.editLoadingValue = false// 关闭loading框
          this.dialogFormVisibleValue = false// 关闭表单页面
          this.initDataValueForm()// 重置表单数据
          var id = this.searchParamValue.SEARCH_LIKE_speckey_id
          this.getList(id, '')// 重新获取表格数据
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
        })
      })
    },
    // 【属性值 按钮点击】---删除行
    handleDeleteValue(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        // 【API请求】---- 删除数据
        deleteMSpecValueInfo({ ids: row.id }).then(() => {
          this.$message({ message: '操作成功', type: 'success' })
          var id = this.searchParamValue.SEARCH_LIKE_speckey_id
          this.getList(id, '')// 重新获取表格数据
        })
      }).catch(() => {
      })
    },

    // 关闭表单弹出框
    closeDialogValue() {
      this.$refs['dataValueForm'].resetFields()
      this.dialogFormVisibleValue = false
    },

    // 关闭规格组key列表页
    valueListCloseDialog() {
      this.getValueListVisible = false
    },

    // 初始化表单数据，重新渲染
    initDataForm() {
      this.dataForm = {
        id: '',
        classifyId: null,
        specGroupId: '',
        specName: '',
        specType: '',
        showPosition: '',
        searchFlag: '',
        orderNo: '',
        remark: '',
        createTime: '',
        createUser: '',
        updateTime: '',
        updateUser: ''
      }
      this.fileList = []
    },
    // 初始化spu属性值表单数据，重新渲染
    initDataValueForm() {
      this.dataValueForm = {
        id: '',
        speckeyId: '',
        value: '',
        remark: ''
      }
      Object.assign(this.$data.dataValueForm, this.$options.data().dataValueForm)
      this.fileList = []
    },
    // 关闭表单弹出框
    closeDialog() {
      this.$refs['dataForm'].resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>
